var displayScreenJs = function(targetObj){
	this.sysUtils = new kendoUtils();
	this.targetObj = targetObj;
	this.init();
	return this;
};

displayScreenJs.prototype={
		init:function(){
			var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
			var bindFunctionArray = [{fName:"saveequOrg",fs:this.saveequOrg},{fName:"cancel",fs:parkTree.resetTree}];
			this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
			this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
			this.initNumberWidget();
		},
		/**
		 * 创建数字化控件
		 */
		createNumericTextBox:function(div,minNumber,maxNumber){
			div.kendoNumericTextBox({
				min:minNumber,
				max:maxNumber,
				format:"#"
//                change:this.changeDeviceNumberByInput,
//                spin:this.changeDeviceNumberBySpin
			});
		},
		/**
		 * 初始化数字控件
		 */
		initNumberWidget:function(){
			/*this.createNumericTextBox(this.targetObj.find("#displayScreenNo"),1,4);
			this.createNumericTextBox(this.targetObj.find("#deviceNumber"),1,99);
			this.createNumericTextBox(this.targetObj.find("#deviceport"),0,65535);*/
			//判断是挂在设备下还是独立使用
			var selectedNodes = zTree.getSelectedNodes()[0];
			var node = selectedNodes.getParentNode();  //父节点
			var bz,ck,chnType = "";
			if(node.gpc == "equ"){   //如果父节点为设备，说明走的是板载串口，不用设置机号
				this.targetObj.find("#changeEquIp").remove();
				bz = Utils.getModelValueByModelKey(BASIS.SCREEN_TYPE.SCREEN_TYPE_1);
				ck = Utils.getModelValueByModelKey(BASIS.COMM_METHODS.COMM_METHODS_1);
                this.sysUtils.mvvmModel.set("equipmentManagements_basic_SerialPoryName",'uart3');
                chnType=node.getParentNode().gpc;
			}else{
                this.sysUtils.mvvmModel.set("equipmentManagements_basic_port",5000);
                this.targetObj.find("#changeEquPoryName").remove();
				bz = Utils.getModelValueByModelKey(BASIS.SCREEN_TYPE.SCREEN_TYPE_3);
				ck = Utils.getModelValueByModelKey(BASIS.COMM_METHODS.COMM_METHODS_2);
                chnType=node.gpc;
			}
            var displayScreenNo = parkTree.downDevJson["deviceType"] == 6? 2:(parkTree.downDevJson["deviceType"] == 8? 4:1);
			if(displayScreenNo==4){
				if(chnType=='chn_in'){

				}else{

				}
			}else if(displayScreenNo==2){
                if(chnType=='chn_in'){

                }else{

                }
			}else if(displayScreenNo==1){
                if(chnType=='chn_in'){

                }else{

                }
			}
			Utils.createDataDictDropDownList([{div:this.targetObj.find("#displayScreenType"),parentModelKey:BASIS.SCREEN_TYPE.ROOT,select:bz},
			                                  {div:this.targetObj.find("#commMethods"),parentModelKey:BASIS.COMM_METHODS.ROOT,select:ck}], this, this.loadEquinfo);
		},
		changeDeviceNumberByInput:function () {
			var no = this.value();
			var _this = this;
			var code = displayScreen.editResult.organizationCode == undefined ? '':displayScreen.editResult.organizationCode;
			JsonpAjax(baseApiPath+'facilities/checkCtrlNo?ctrlNo='+no+'&organizationCode='+code,null,"json",function(result){
				if(result.success){
					layer.tips('设备机号'+no+'已存在！！！',_this._text[0],{tipsMore: true,tips: [2, '#78BA32']});
					_this._text[0].value = '';
				}
			});
		},
		changeDeviceNumberBySpin:function () {
			var no = this.value();
			var _this = this;
			var code = displayScreen.editResult.organizationCode == undefined ? '':displayScreen.editResult.organizationCode;
			JsonpAjax(baseApiPath+'facilities/checkCtrlNo?ctrlNo='+no+'&organizationCode='+code,null,"json",function(result){
				if(result.success){
					layer.tips('设备机号'+no+'已存在！！！',_this.element[0],{tipsMore: true,tips: [2, '#78BA32']});
					_this.element[0].value = '';
				}
			});
		},
		loadEquinfo:function (e) {
			//不让用户选择显示屏类型和通讯方式
			this.targetObj.find("#displayScreenType").data("kendoDropDownList").enable(false);
			this.targetObj.find("#commMethods").data("kendoDropDownList").enable(false);
			var that = this;
			that.editResult = null;
			that.isEdit = null;
			if (!isEmpty(e)) {
				that = $(e.target).data("that");
			}
			var selectedNodes = zTree.getSelectedNodes()[0];
			var node = selectedNodes.getParentNode();  //父节点
			var chnType='';
			if(node.gpc=='equ'){
                chnType=node.getParentNode().gpc;
			}else{
                chnType=node.gpc;
			}
			var id = selectedNodes.id,level=selectedNodes.level;
			var url = baseApiPath+"facilities/parkInfo";
			if(id){
				that.isEdit = true;
				url += '?isEdit='+that.isEdit+'&organizationCode='+id;
			}else{
				that.isEdit = false;
				url = baseApiPath+'facilities/addOrz?isEdit='+that.isEdit+'&pid='+node.id+'&lev='+level+'&gpc=equ';
			}
			//显示相应的设备信息
			Utils.createAjaxDropDownList({div:that.targetObj.find("#deviceType"),select:[{code:parkTree.downDevJson["deviceType"],name:parkTree.downDevJson["deviceName"]}],unshift:false},that);
			that.sysUtils.mvvmModel.set("equipmentManagements_basic_deviceid",parkTree.downDevJson["deviceType"]);
			//四行显示屏屏幕数量为4，一体机屏幕数量为2，其余显示屏为1
			var displayScreenNo = parkTree.downDevJson["deviceType"] == 6? 2:(parkTree.downDevJson["deviceType"] == 8? 4:1);
			that.sysUtils.mvvmModel.set("equipmentManagements_basic_displayScreenNo",displayScreenNo);
			for(var i = 0;i<displayScreenNo;i++){
				that.createTr(that.targetObj.find("#normal_list"), null, i,chnType,displayScreenNo);
				that.createTr(that.targetObj.find("#leisure_list"),  null, i,null,displayScreenNo);
			}
			JsonpAjax(url,null,"json",function(result){
				that.editResult = result;
				that.sysUtils.setVariantBindFieldValue(result);
				var fullTableTr = function(jsonObj,id){
					var table = that.targetObj.find("#"+id);
					var trIndex = 0;
					for(var names in jsonObj){
						//leisureContent  jobContent  displayMode displayColor displaySpeed
						trIndex = parseInt(names.substring(names.length - 1,names.length)) + 1;
						var tr = table.find("tr:eq("+trIndex+")");
						var json = JSON.parse(jsonObj[names]);
						if(id == "leisure_list"){
							tr.find("input[name='leisureContent']").data("kendoMultiSelect").value(json["displayContent"].split(","));
						}else{
							tr.find("input[name='jobContent']").data("kendoMultiSelect").value(json["displayContent"].split(","));
						}
						tr.find("input[name='displayMode']").data("kendoDropDownList").value(json["displayMode"]);
						tr.find("input[name='displayColor']").data("kendoDropDownList").value(json["displayColor"]);
						tr.find("input[name='displaySpeed']").data("kendoNumericTextBox").value(parseInt(json["displaySpeed"]));
						tr.find("input[name='displayTime']").data("kendoNumericTextBox").value(parseInt(json["displayTime"]));
						tr.find("input[name='displayRollTimes']").data("kendoNumericTextBox").value(parseInt(json["displayRollTimes"]));
					}
				}
				//填充table列表
				var leisureList = result["equipmentManagements"]["basic"]["leisureList"];
				if(isNotEmpt(leisureList)){
					fullTableTr(leisureList, "leisure_list");
				}
				var normalList = result["equipmentManagements"]["basic"]["normalList"];
				if(isNotEmpt(normalList)){
					fullTableTr(normalList, "normal_list");
				}
			},false);
			parkTree.hideMask();
		},
		//保存设备信息
		saveequOrg:function(e){
			var that =  $(e.target).data("that");
			if(validityForm(that.targetObj)){
				var parkInfo = that.sysUtils.getBindDocJson();
				var url = baseApiPath+"facilities/saveParkInfo?isEdit=" + that.isEdit;
				var sendJson=parkInfo;
				for(var o in parkInfo){
					if(o.indexOf('_') !=-1){
						var strArray = o.substring(o.indexOf('_')+1);
						var name = o.substring(0, o.indexOf('_'));
						var jsonA = {};
						jsonA[name] = that.recursionObjectName(strArray,parkInfo[o]) ;
						delete parkInfo[o];
						sendJson =$.extend(true,parkInfo,jsonA);
					}
				}
                var flag = false;
				//处理空闲显示内容  jobContent   leisureContent    displayMode  displayColor  displaySpeed
				that.targetObj.find("#leisure_list").find("tr:not(:first)").each(function(index,item){
					var json = {};
                    json["displayContent"] = $(item).find("input[name='leisureContent']").data("kendoMultiSelect").value();
                    json["displayMode"] = $(item).find("input[name='displayMode']").val();
                    json["displayColor"] = $(item).find("input[name='displayColor']").val();
                    json["displaySpeed"] = $(item).find("input[name='displaySpeed']").val();
                    json["displayTime"] = $(item).find("input[name='displayTime']").val();
                    json["displayRollTimes"] = $(item).find("input[name='displayRollTimes']").val();
                    json["serialNo"] = index;
                    json["displayContent"] = json["displayContent"].toString();
                    if(null == sendJson["equipmentManagements"]["basic"]["leisureList"]){
                        sendJson["equipmentManagements"]["basic"]["leisureList"] = {};
                    }
                    sendJson["equipmentManagements"]["basic"]["leisureList"]["leisure" + index] = JSON.stringify(json);
					/*json["displayContent"] = $(item).find("input[name='leisureContent']").data("kendoMultiSelect").value();
					if(isNotEmpt(json["displayContent"])){
						json["displayMode"] = $(item).find("input[name='displayMode']").val();
						json["displayColor"] = $(item).find("input[name='displayColor']").val();
						json["displaySpeed"] = $(item).find("input[name='displaySpeed']").val();
						json["displayTime"] = $(item).find("input[name='displayTime']").val();
						json["displayRollTimes"] = $(item).find("input[name='displayRollTimes']").val();
						json["serialNo"] = index;
                        json["displayContent"] = json["displayContent"].toString();
						if(null == sendJson["equipmentManagements"]["basic"]["leisureList"]){
							sendJson["equipmentManagements"]["basic"]["leisureList"] = {};
						}
						sendJson["equipmentManagements"]["basic"]["leisureList"]["leisure" + index] = JSON.stringify(json);
					}else{
                        flag = true;
                        layer.tips('请注意,这里不能留空哦~!',$(item).find("input[name='leisureContent']").parent(),{tipsMore: true,tips: [2, '#78BA32']});
                        return;
					}*/
				});
				//处理工作显示内容

				that.targetObj.find("#normal_list").find("tr:not(:first)").each(function(index,item){
					var json = {};
                    json["displayContent"] = $(item).find("input[name='jobContent']").data("kendoMultiSelect").value();
                    json["displayMode"] = $(item).find("input[name='displayMode']").val();
                    json["displayColor"] = $(item).find("input[name='displayColor']").val();
                    json["displaySpeed"] = $(item).find("input[name='displaySpeed']").val();
                    json["displayRollTimes"] = $(item).find("input[name='displayRollTimes']").val();
                    json["displayTime"] = $(item).find("input[name='displayTime']").val();
                    json["serialNo"] = index;
                    json["displayContent"] = json["displayContent"].toString();
                    if(null == sendJson["equipmentManagements"]["basic"]["normalList"]){
                        sendJson["equipmentManagements"]["basic"]["normalList"] = {};
                    }
                    sendJson["equipmentManagements"]["basic"]["normalList"]["normal" + index] = JSON.stringify(json);
                    /*json["displayContent"] = $(item).find("input[name='jobContent']").data("kendoMultiSelect").value();
                    if(isNotEmpt(json["displayContent"])){
                        json["displayContent"] = $(item).find("input[name='jobContent']").data("kendoMultiSelect").value();
                        json["displayMode"] = $(item).find("input[name='displayMode']").val();
                        json["displayColor"] = $(item).find("input[name='displayColor']").val();
                        json["displaySpeed"] = $(item).find("input[name='displaySpeed']").val();
                        json["displayRollTimes"] = $(item).find("input[name='displayRollTimes']").val();
                        json["displayTime"] = $(item).find("input[name='displayTime']").val();
                        json["serialNo"] = index;
                        json["displayContent"] = json["displayContent"].toString();
                        if(null == sendJson["equipmentManagements"]["basic"]["normalList"]){
                            sendJson["equipmentManagements"]["basic"]["normalList"] = {};
                        }
                        sendJson["equipmentManagements"]["basic"]["normalList"]["normal" + index] = JSON.stringify(json);
                    }else{
                        flag = true;
                        layer.tips('请注意,这里不能留空哦~!',$(item).find("input[name='jobContent']").parent(),{tipsMore: true,tips: [2, '#78BA32']});
                        return;
                    }*/
				});
				/*if(flag){  //如果校验不通过返回
					return;
				}*/
				JsonpAjax(url,JSON.stringify(sendJson),"json",function(result){
                        that.sysUtils.setVariantBindFieldValue(result);
                    	that.isEdit=true;
                        parkTree.localUpdate(result);
				});
			}
		},
		recursionObjectName : function (strArray,val) {
			if (strArray.indexOf('_') == -1){
				var jsonC = {};
				jsonC[strArray]=val;
				return jsonC;
			}
			var name = strArray.substring(0, strArray.indexOf('_'));
			strArray = strArray.substring(strArray.indexOf('_')+1);
			var jsonB = {};
			jsonB[name] = this.recursionObjectName(strArray,val);
			return jsonB;
		},
		/**
		 * 公共的创建行数据
		 */
		createTr:function(table,dataJson,index,chnType,displayScreenNo){
			var tr = $("<tr></tr>");
			tr.append($("<td>"+(index+1)+"</td>"));
			var td = $("<td><input style='width:99.5%;height:99%;'></input></td>");
			if(table.attr("id") == "leisure_list"){  //空闲时间
				td.find("input").attr("name","leisureContent");
                td.find("input").kendoMultiSelect({
                    placeholder:"--请选择--",
                    dataTextField:"modelName",
                    dataValueField:"modelValue",
                    dataSource:Utils.getModelArrayByParentModelKey(BASIS.ADVERTISING_CONTENT.ROOT)
                });
                if(index==0){
                    td.find("input[name='leisureContent']").data("kendoMultiSelect").value('0,1'.split(","));
				}else{
                    td.find("input[name='leisureContent']").data("kendoMultiSelect").value((index+1+'').split(","));
                }
                /*if(displayScreenNo==1){//单行屏
                    td.find("input").data("kendoMultiSelect").value('1'.split(","));
				}else if(displayScreenNo==2){//两行屏
                    td.find("input").data("kendoMultiSelect").value('1'.split(","));
				}else if(displayScreenNo==2){
                    if(index==1){
                        td.find("input").data("kendoMultiSelect").value('1'.split(","));
                    }else if(index==2){
                        td.find("input").data("kendoMultiSelect").value('2'.split(","));
                    }else if(index==3){
                        td.find("input").data("kendoMultiSelect").value('3'.split(","));
                    }else if(index==4){
                        td.find("input").data("kendoMultiSelect").value('4'.split(","));
                    }
				}*/
				//Utils.createDataDictDropDownList({div:td.find("input"),parentModelKey:BASIS.ADVERTISING_CONTENT.ROOT}, this);
			}else{    							//正常时间
				td.find("input").attr("name","jobContent");
				//创建显示区域内容
				td.find("input").kendoMultiSelect({  
					placeholder:"--请选择--",
					dataTextField:"modelName",
					dataValueField:"modelValue",
					dataSource:Utils.getModelArrayByParentModelKey(BASIS.DISPLAY_CONTENT.ROOT)
				});
				if(displayScreenNo==1){//单行屏
					if(chnType=='chn_in'){//入口
                        td.find("input[name='jobContent']").data("kendoMultiSelect").value('3,2,0,4,5,6,10'.split(","));
					}else{//出口
                        td.find("input[name='jobContent']").data("kendoMultiSelect").value('3,2,1,4,5,6,10'.split(","));
					}
				}else if(displayScreenNo==2){//两行屏
					if(index==0){
                        td.find("input[name='jobContent']").data("kendoMultiSelect").value('6,9'.split(","));
					}else{
                        if(chnType=='chn_in'){//入口
                            td.find("input[name='jobContent']").data("kendoMultiSelect").value('3,2,0,4,5,10'.split(","));
                        }else{//出口
                            td.find("input[name='jobContent']").data("kendoMultiSelect").value('3,2,1,4,5,10'.split(","));
                        }
					}
				}else if(displayScreenNo==4){
					if(index==1){
                        td.find("input[name='jobContent']").data("kendoMultiSelect").value('6,9'.split(","));
					}else if(index==3){
                        if(chnType=='chn_in'){//入口
                            td.find("input[name='jobContent']").data("kendoMultiSelect").value('3,2,0,4,5,10'.split(","));
                        }else{//出口
                            td.find("input[name='jobContent']").data("kendoMultiSelect").value('3,2,1,4,5,10'.split(","));
                        }
					}
				}

			} 
			tr.append(td);
			//显示方式
			var displayMode =  $("<td><input name='displayMode' style='width:99.5%'></input></td>");
			Utils.createDataDictDropDownList({div:displayMode.find("input"),parentModelKey:BASIS.DISPLAY_MODE.ROOT,unshift:true}, this);
			tr.append(displayMode);
			//显示颜色
			var displayColor =  $("<td><input name='displayColor' style='width:99.5%'></input></td>");
			Utils.createDataDictDropDownList({div:displayColor.find("input"),parentModelKey:BASIS.DISPLAY_COLOR.ROOT,unshift:true}, this);
			tr.append(displayColor);
			//显示速度
			var displaySpeed = $("<td><input name='displaySpeed' style='width:99.5%' value='100'></input></td>");
			this.createNumericTextBox(displaySpeed.find("input"),1,100);
			tr.append(displaySpeed);
			//显示时间
			var displayRollTimes = $("<td><input name='displayRollTimes' style='width:99.5%' value='-1'></input></td>");
			this.createNumericTextBox(displayRollTimes.find("input"),-1,99999);
			tr.append(displayRollTimes);
			//滚动次数
			var displayTime = $("<td><input name='displayTime' style='width:99.5%' value='-1'></input></td>");
			this.createNumericTextBox(displayTime.find("input"),-1,99999);
			tr.append(displayTime);
			table.find("tbody").append(tr);
		}
}
